/**
 * 
 */
package com.pmsco.pms.business;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pmsco.pms.business.dto.CategoryRequestDTO;
import com.pmsco.pms.business.dto.CategoryReturnDTO;
import com.pmsco.pms.business.dto.SubCategoryRequestDTO;
import com.pmsco.pms.business.dto.SubCategoryReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.core.domain.Category;
import com.pmsco.pms.core.domain.SubCategory;
import com.pmsco.pms.services.impl.CategoryServiceImpl;
import com.pmsco.pms.services.impl.SubCategoryServiceImpl;

/**
 * @author Long Tran
 *
 */
public class CategoryBusiness {
	private static final Logger logger = LoggerFactory.getLogger(CategoryBusiness.class);
	private CategoryServiceImpl categoryService = new CategoryServiceImpl();
	private SubCategoryServiceImpl subCategoryService = new SubCategoryServiceImpl();
	
	public CategoryReturnDTO listCategoryData(CategoryRequestDTO categoryRequestDTO) {
		logger.info("Start listCategoryData");
		CategoryReturnDTO categoryReturnDTO = new CategoryReturnDTO();
		
		List<Category> categories = categoryService.listCategory(categoryRequestDTO.getSearchField(), 
				categoryRequestDTO.getSearchString(), 
				categoryRequestDTO.getSearchOper(), 
				categoryRequestDTO.getSidx(), 
				categoryRequestDTO.getSord(), 
				categoryRequestDTO.getPage(), 
				categoryRequestDTO.getRows());
		Integer categoriesSize = categoryService.countListCategory(categoryRequestDTO.getSearchField(), 
				categoryRequestDTO.getSearchString(), 
				categoryRequestDTO.getSearchOper());
		
		categoryReturnDTO.setCategories(categories);
		categoryReturnDTO.setCategoriesSize(categoriesSize);
		
		logger.info("End listCategoryData");
		return categoryReturnDTO;
	}
	
	public CategoryReturnDTO editCategory(CategoryRequestDTO categoryRequestDTO) {
		logger.info("Start editCategory");
		CategoryReturnDTO categoryReturnDTO = new CategoryReturnDTO();
		
		categoryReturnDTO.setCategory(categoryService.getCategoryById(categoryRequestDTO.getCategoryId()));
		categoryReturnDTO.setReturnView(PmsConstant.editCategory_View);
		
		logger.info("End editCategory");
		return categoryReturnDTO;
	}
	
	public CategoryReturnDTO createCategory(CategoryRequestDTO categoryRequestDTO) {
		logger.info("Start createCategory");
		CategoryReturnDTO categoryReturnDTO = new CategoryReturnDTO();
		
		categoryReturnDTO.setCategory(new Category());
		categoryReturnDTO.setReturnView(PmsConstant.createCategory_View);
		
		logger.info("End createCategory");
		return categoryReturnDTO;
	}
	
	public CategoryReturnDTO saveCategory(CategoryRequestDTO categoryRequestDTO) {
		logger.info("Start saveCategory");
		CategoryReturnDTO categoryReturnDTO = new CategoryReturnDTO();
		
		logger.debug("Check if category name existed");
		Category dirtyCategory = categoryRequestDTO.getDirtyCategory();
		Category existedCategory = categoryService.getCategoryByName(dirtyCategory.getName());
		
		logger.debug("Category name: " + dirtyCategory.getName());
		logger.debug("Id: " + String.valueOf(dirtyCategory.getId()));
		if((existedCategory != null && dirtyCategory.getId() == null) || (existedCategory != null && existedCategory.getId().compareTo(dirtyCategory.getId()) != 0)) {
			categoryReturnDTO.setReturnView(PmsConstant.editCategory_View);
			categoryReturnDTO.setCategory(dirtyCategory);
			categoryReturnDTO.setErrorMessage(PmsConstant.categoryName_Existed_Key);
		} else {
			categoryService.saveOrUpdateCategory(dirtyCategory);
			categoryReturnDTO.setReturnView(PmsConstant.listCategory_View);
		}
		logger.info("End saveCategory");
		return categoryReturnDTO;
	}
	
	public String deleteCategories(String selectedCategoryIds) {
		String cannotDeleteCategoryIds = "";
		logger.info("Start deleteCategories");
		if(selectedCategoryIds != null && !selectedCategoryIds.isEmpty()) {
			String[] categoryIds = selectedCategoryIds.split(",");
			for(int i = 0; i < categoryIds.length; i++) {
				Long id = Long.valueOf(categoryIds[i]);
				Category category = categoryService.getCategoryById(id);
				if(category.getMedicineCategories().size() > 0 || category.getSubCategories().size() > 0)
					cannotDeleteCategoryIds += categoryIds[i] + " ";
				else
					categoryService.deleteCategory(category);
			}
		}
		logger.info("End deleteCategories");
		return cannotDeleteCategoryIds;
	}
	
	
	public SubCategoryReturnDTO listSubCategoryData(SubCategoryRequestDTO subCategoryRequestDTO) {
		logger.info("Start listSubCategoryData");
		SubCategoryReturnDTO subCategoryReturnDTO = new SubCategoryReturnDTO();
		
		List<SubCategory> subCategories = subCategoryService.listSubCategory(subCategoryRequestDTO.getSearchField(), 
				subCategoryRequestDTO.getSearchString(), 
				subCategoryRequestDTO.getSearchOper(), 
				subCategoryRequestDTO.getSidx(), 
				subCategoryRequestDTO.getSord(), 
				subCategoryRequestDTO.getPage(), 
				subCategoryRequestDTO.getRows());
		Integer subCategoriesSize = subCategoryService.countListSubCategory(subCategoryRequestDTO.getSearchField(), 
				subCategoryRequestDTO.getSearchString(), 
				subCategoryRequestDTO.getSearchOper());
		
		subCategoryReturnDTO.setSubCategories(subCategories);
		subCategoryReturnDTO.setSubCategoriesSize(subCategoriesSize);
		
		logger.info("End listSubCategoryData");
		return subCategoryReturnDTO;
	}
	
	public SubCategoryReturnDTO editSubCategory(SubCategoryRequestDTO subCategoryRequestDTO) {
		logger.info("Start editSubCategory");
		SubCategoryReturnDTO subCategoryReturnDTO = new SubCategoryReturnDTO();
		
		subCategoryReturnDTO.setSubCategory(subCategoryService.getSubCategoryById(subCategoryRequestDTO.getSubCategoryId()));
		subCategoryReturnDTO.setCategoryList(categoryService.listCategory(new Category()));
		subCategoryReturnDTO.setReturnView(PmsConstant.editSubCategory_View);
		
		logger.info("End editSubCategory");
		return subCategoryReturnDTO;
	}
	
	public SubCategoryReturnDTO createSubCategory(SubCategoryRequestDTO subCategoryRequestDTO) {
		logger.info("Start createSubCategory");
		SubCategoryReturnDTO subCategoryReturnDTO = new SubCategoryReturnDTO();
		
		subCategoryReturnDTO.setSubCategory(new SubCategory());
		subCategoryReturnDTO.setCategoryList(categoryService.listCategory(new Category()));
		subCategoryReturnDTO.setReturnView(PmsConstant.createSubCategory_View);
		
		logger.info("End createSubCategory");
		return subCategoryReturnDTO;
	}
	
	public SubCategoryReturnDTO saveSubCategory(SubCategoryRequestDTO subCategoryRequestDTO) {
		logger.info("Start saveSubCategory");
		SubCategoryReturnDTO subCategoryReturnDTO = new SubCategoryReturnDTO();
		
		logger.debug("Check if sub category name existed");
		SubCategory dirtySubCategory = subCategoryRequestDTO.getDirtySubCategory();
		SubCategory existedSubCategory = subCategoryService.getSubCategoryByName(dirtySubCategory.getName());
		
		logger.debug("SubCategory name: " + dirtySubCategory.getName());
		logger.debug("Id: " + String.valueOf(dirtySubCategory.getId()));
		if((existedSubCategory != null && dirtySubCategory.getId() == null) || (existedSubCategory != null && existedSubCategory.getId().compareTo(dirtySubCategory.getId()) != 0)) {
			subCategoryReturnDTO.setReturnView(PmsConstant.editSubCategory_View);
			subCategoryReturnDTO.setSubCategory(dirtySubCategory);
			subCategoryReturnDTO.setErrorMessage(PmsConstant.subCategoryName_Existed_Key);
		} else {
			Category category = categoryService.getCategoryById(subCategoryRequestDTO.getCategoryId());
			dirtySubCategory.setCategory(category);
			subCategoryService.saveOrUpdateSubCategory(dirtySubCategory);
			subCategoryReturnDTO.setReturnView(PmsConstant.listSubCategory_View);
		}
		logger.info("End saveSubCategory");
		return subCategoryReturnDTO;
	}
	
	public String deleteSubCategories(String selectedSubCategoryIds) {
		String cannotDeleteSubCategoryIds = "";
		logger.info("Start deleteSubCategories");
		if(selectedSubCategoryIds != null && !selectedSubCategoryIds.isEmpty()) {
			String[] subCategoryIds = selectedSubCategoryIds.split(",");
			for(int i = 0; i < subCategoryIds.length; i++) {
				Long id = Long.valueOf(subCategoryIds[i]);
				SubCategory category = subCategoryService.getSubCategoryById(id);
				if(category.getCategory().getMedicineCategories().size() > 0)
					cannotDeleteSubCategoryIds += subCategoryIds[i] + " ";
				else
					subCategoryService.deleteSubCategory(category);
			}
		}
		
		logger.info("End deleteCategories");
		return cannotDeleteSubCategoryIds;
	}
}
